﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoFlowStatistics.Dialogs
{
    public partial class ImageComb : Form
    {
        public ImageComb()
        {
            InitializeComponent();
        }

        private void ImageComb_Load(object sender, EventArgs e)
        {
            string[] comb = Properties.Settings.Default.ImageComb.Split('|');
            string[] picOne=comb[0].Split('@');
            txtWidthOne.Text = picOne[0];
            txtHeightOne.Text = picOne[1];
            txtPointOne.Text = picOne[2];
            string[] picTwo = comb[1].Split('@');
            txtWidthTwo.Text = picTwo[0];
            txtHeightTwo.Text = picTwo[1];
            txtPointTwo.Text = picTwo[2];
            if (comb[2] == "1")
            {
                rdHori.Checked = true;
            }
            else
            {
                rdVert.Checked = true;
            }
        }

        #region 图片1

        Point oneStartPoint = new Point();
        bool isDrag=false;
        private void picOne_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((isDrag = !isDrag) == true)
                {
                    txtPointOne.Text = e.X + "," + e.Y;
                    oneStartPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void picOne_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics OneP = picOne.CreateGraphics();
            
            if (isDrag)
            {
                OneP.DrawRectangle(new Pen(Color.Red, 1), oneStartPoint.X, oneStartPoint.Y, e.X - oneStartPoint.X, e.Y - oneStartPoint.Y);
                OneP.Dispose();
            }
        }

        private void picOne_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
            this.txtWidthOne.Text = (e.X - oneStartPoint.X).ToString();
            this.txtHeightOne.Text = (e.Y - oneStartPoint.Y).ToString();
        }

        #endregion

        #region 图2

        private void picTwo_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((isDrag = !isDrag) == true)
                {
                    txtPointTwo.Text = e.X + "," + e.Y;
                    oneStartPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void picTwo_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics TwoP = picTwo.CreateGraphics();

            if (isDrag)
            {
                TwoP.DrawRectangle(new Pen(Color.Red, 1), oneStartPoint.X, oneStartPoint.Y, e.X - oneStartPoint.X, e.Y - oneStartPoint.Y);
                TwoP.Dispose();
            }
        }

        private void picTwo_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
            this.txtWidthTwo.Text = (e.X - oneStartPoint.X).ToString();
            this.txtHeightTwo.Text = (e.Y - oneStartPoint.Y).ToString();

        }
        #endregion

        //加载图片
        private void btnLoldImg_Click(object sender, EventArgs e)
        {
            this.picOne.Image = Class.ImageComb.ReducedImage(Class.AppClass.ImageOne, 364, 258);
            this.picTwo.Image = Class.ImageComb.ReducedImage(Class.AppClass.ImageTwo, 364, 258);
            Class.AppClass.IsLoad = false;
        }

        //合成图片
        private void btnCombImg_Click(object sender, EventArgs e)
        {
            Image imgComb = Class.ImageComb.ImageProcess((Bitmap)picOne.Image, (Bitmap)picTwo.Image, int.Parse(txtWidthOne.Text), int.Parse(txtHeightOne.Text)
                , int.Parse(txtPointOne.Text.Split(',')[0]), int.Parse(txtPointOne.Text.Split(',')[1]),
                int.Parse(txtWidthTwo.Text), int.Parse(txtHeightTwo.Text), int.Parse(txtPointTwo.Text.Split(',')[0])
                , int.Parse(txtPointTwo.Text.Split(',')[1]), this.rdHori.Checked ? "1" : "2");
            picComb.Image = imgComb;//Class.ImageComb.ReducedImage((Bitmap)imgComb, 364, 258);

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.ImageComb = txtWidthOne.Text + "@" + txtHeightOne.Text + "@" + txtPointOne.Text + "|"
                + txtWidthTwo.Text + "@" + txtHeightTwo.Text + "@" + txtPointTwo.Text + "|" + (rdHori.Checked == true ? "1" : "2");
            Properties.Settings.Default.Save();  
        }
    }
}
